﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace VietStar.Library.Commom.Module
{
    public class Cart
    {
        public Cart()
        {
            TotalPrice = 0;
            CartData = new Hashtable();
        }

        public Hashtable CartData
        {
            get;
            set;
        }

        public double TotalPrice
        {
            get;
            set;
        }

        public bool AddItem(int id, string name, double price)
        {
            bool flag = false;
            try
            {
                if (CartData[id] == null)
                {
                    CartItem item = new CartItem(id, name, 1, price);
                    CartData.Add(id, item);
                    //TotalPrice += item.Price;
                    double total = item.Quantity * item.Price;
                    TotalPrice += total;
                    flag = true;
                }
                else
                {
                    CartItem item = (CartItem)CartData[id];
                    item.Quantity += 1;
                    TotalPrice += item.Price;
                    flag = true;
                }
            }
            catch (Exception ex)
            { }
            return flag;
        }

        public bool RemoveItem(int id)
        {
            bool flag = false;
            try
            {
                if (CartData[id] != null)
                {
                    CartItem item = (CartItem)CartData[id];
                    TotalPrice -= (item.Price * item.Quantity);
                    CartData.Remove(id);
                    flag = true;
                }
            }
            catch (Exception ex)
            { }
            return flag;
        }

        public bool Update(int id, int quantity)
        {
            bool flag = false;
            try
            {
                if (CartData[id] != null)
                {
                    CartItem item = (CartItem)CartData[id];
                    TotalPrice -= (item.Price * item.Quantity);
                    item.Quantity = quantity;
                    TotalPrice += (item.Price * item.Quantity);
                    flag = true;
                }
            }
            catch (Exception ex)
            { }
            return flag;
        }
    }
}
